package com.etc.admin.service.impl;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.etc.admin.dao.UserMapper;
import com.etc.admin.service.UserService;
import com.etc.admin.util.PageData;
import com.etc.entity.User;

/**
 * 
 * @author 李添杭
 *
 */

@Service(value="userService")
public class UserServiceImpl implements UserService {
	@Resource
	private UserMapper userMapper;

	@Override
	public boolean deleteByPrimaryKey(Integer userid) {
		
		return userMapper.deleteByPrimaryKey(userid)>0;
	}

	@Override
	public boolean insert(User record) {
		// TODO Auto-generated method stub
		return userMapper.insert(record)>0;
	}

	@Override
	public boolean insertSelective(User record) {
		// TODO Auto-generated method stub
		return userMapper.insertSelective(record)>0;
	}

	@Override
	public User selectByPrimaryKey(Integer userid) {
		// TODO Auto-generated method stub
		return userMapper.selectByPrimaryKey(userid);
	}

	@Override
	public boolean updateByPrimaryKeySelective(User record) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKeySelective(record)>0;
	}

	@Override
	public boolean updateByPrimaryKey(User record) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKey(record)>0;
	}

	@Override
	public PageData<User> selectUserByPage(Integer page, Integer pageSize, String content) {
		Integer count = userMapper.selectCount(content);
		
		//得到起始位置
		int start = (page - 1) * pageSize;
	
		//这里用的是分页数据
		List<User> list = userMapper.selectUserByPage(start, pageSize, content);
			
		//构造一个PageData
		PageData<User> data = new PageData<User>(list, count, pageSize, page);
	
		return data;
	}
	
	//根据用户名得到用户，判断这个用户是否已经存在（蒋丽娟)
	@Override
	public int getUserIdByname(User user) {
		User user1=userMapper.getUserByname(user.getUsername());//根据用户名得到用户，判断这个用户是否已经存在
	if(user1!=null) {
		return user1.getUserid();//如果存在，返回用户的id
	}else {//用户名不存在，插入用户
		int i=userMapper.insertSelective(user);
		if(i>0) {
		return	userMapper.getUserByname(user.getUsername()).getUserid();
		}
	}
		return 0;
	}

	@Override
	public int userCount() {
		// TODO Auto-generated method stub
		return userMapper.userCount();
	}
	

	
	
	
}
